﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using cszmcaux;
using System.Net.NetworkInformation;

namespace HTCP_Scan_Frame
{
    public partial class Form1 : Form
    {
        public int AlarmShowTime = 5 * 40;
        public int AlarmShowTimeNum = 0;
        public int Scan_time_ms = 3;
        public static bool g_HomeX = false;
        public static bool g_HomeY = false;
        public static bool g_HomeZ = false;
        public static string g_Alarm = "";
        public static string g_XSrevoOn = "";
        public static string g_YSrevoOn = "";
        public static string g_XHomeDone = "";
        public static string g_YHomeDone = "";
        public static IntPtr g_handle;         //链接返回的句柄，可以作为卡号
        public float Bus_Connect = -1;     //BAS文件中变量判断总线类连接成功
        public bool  Bus_ConnectRead = false;
        public bool[] ifHome = new bool[3];
        RemoteControl RC;

        public byte[] AxisStatiusNow = new byte[3] { 0x1B, 0x09, 0x09 };

        private Thread WriteThread;

        public string myIP = "0.0.0.0";
        public string myIP2 = "0.0.0.0";
        public Form1()
        {
            InitializeComponent();
            scrollingText_Notioce.ScrollText = null;
            myIP = GetLocalIP();
            myIP2 = GetLocalIP2();
            RC = new RemoteControl(myIP, myIP2);
            RC.XAxis_Offset = Convert.ToSingle(UpdataReadData("X_Axis_Offset"));
            RC.YAxis_Offset = Convert.ToSingle(UpdataReadData("Y_Axis_Offset"));
            RC.ZAxis_Offset = Convert.ToSingle(UpdataReadData("Z_Axis_Offset"));
            Scan_time_ms = Convert.ToInt16(UpdataReadData("Scan_time_ms"));
            AlarmShowTime = 5 *  Convert.ToInt32(UpdataReadData("Alarm_Show_time_s"));
            
            
            ifHome[0] = false;
            ifHome[1] = false;
            ifHome[2] = false;
            
            RC.StartAxishomeThread();
            RC.Start(myIP, 60310, myIP2, 60310);
            if (RC.IsOpened == true)
            {
                Btn_CON_RM.Image = global::HTCP_Scan_Frame.Properties.Resources.Connect;
                Btn_CON_RM.Text = "连接";
                Btn_CON_RM.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(16)))), ((int)(((byte)(34)))), ((int)(((byte)(44)))));

            }
            else
            {
                Btn_CON_RM.Image = global::HTCP_Scan_Frame.Properties.Resources.Disconnect;
                Btn_CON_RM.Text = "断开";
                Btn_CON_RM.BackColor = Color.Maroon;
            }
            WriteThread = new Thread(WritrToRemove);
            WriteThread.Start();
        }

        public void UpdateWriteData(string strName, string strData)
        {
            //string str = "C:\\PMSK\\Delta\\Delta.ini";
            string str = Environment.CurrentDirectory + "\\QCGK.ini";
            new IniFiles(str).WriteString("Parameter", strName, strData);
        }

        public string UpdataReadData(string strName)
        {
            string str = Environment.CurrentDirectory + "\\QCGK.ini";
            string strRcv = new IniFiles(str).ReadString("Parameter", strName, "0");
            return strRcv;
        }

        public byte GetAxisZStatius()
        {
            byte ReturnStatus = 0;
            byte[] AxisStatius = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
            int AxiaGetStatus = 0;
            float AxisGetstatusHome = 0;

            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//bit0 在线
            if (ret == 0 && Bus_Connect == 1)
            {
                AxisStatius[0] = 1;
            }
            else
            {
                AxisStatius[0] = 0;
            }

            ret = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, 2, ref AxiaGetStatus);  //bit1是否运动（1运动，0停止）

            if (AxiaGetStatus == 0)
            {
                AxisStatius[1] = 0;
            }
            else
            {
                AxisStatius[1] = 2;
            }

            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisZ_HomeDone", ref AxisGetstatusHome);  //bit2:bit3 是否寻零（1正在寻零，2寻零完成） 

            if (AxisGetstatusHome == 1)
            {
                AxisStatius[2] = 8;
            }
            else
            {
                AxisStatius[2] = 4;
            }
            AxisStatius[3] = 0;
            ReturnStatus = (byte)(AxisStatius[0] + AxisStatius[1] + AxisStatius[2] + AxisStatius[3]);

            return ReturnStatus;
        }

        public byte GetAxisYStatius()
        {
            byte ReturnStatus = 0;
            byte[] AxisStatius = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
            int AxiaGetStatus = 0;
            float AxisGetstatusHome = 0;

            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//bit0 在线
            if (ret == 0 && Bus_Connect == 1)
            {
                AxisStatius[0] = 1;
            }
            else
            {
                AxisStatius[0] = 0;
            }

            ret = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, 1, ref AxiaGetStatus);  //bit1是否运动（1运动，0停止）

            if (AxiaGetStatus == 0)
            {
                AxisStatius[1] = 0;
            }
            else
            {
                AxisStatius[1] = 2;
            }

            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisY_HomeDone", ref AxisGetstatusHome);  //bit2:bit3 是否寻零（1正在寻零，2寻零完成） 

            if (AxisGetstatusHome == 1)
            {
                AxisStatius[2] = 8;
            }
            else
            {
                AxisStatius[2] = 4;
            }

            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxis_YSine_MoveDone", ref AxisGetstatusHome);  //bit4:bit5 是否正弦

            if (AxisGetstatusHome == 0)
            {
                AxisStatius[3] = 0;
            }
            else
            {
                AxisStatius[3] = 16;
            }
            ReturnStatus = (byte)(AxisStatius[0] + AxisStatius[1] + AxisStatius[2] + AxisStatius[3]);

            return ReturnStatus;
        }

        public byte  GetAxisXStatius( )
        {
            byte ReturnStatus = 0;
            byte[] AxisStatius = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
            int AxiaGetStatus = 0;
            float AxisGetstatusHome = 0;

            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//bit0 在线
            if (ret == 0 && Bus_Connect == 1)
            {
                AxisStatius[0] = 1;
            }
            else
            {
                AxisStatius[0] = 0;
            }

            ret = zmcaux.ZAux_Direct_GetAxisStatus(g_handle, 0, ref AxiaGetStatus);  //bit1是否运动（1运动，0停止）

            if (AxiaGetStatus == 0)
            {
                AxisStatius[1] = 0;
            }
            else
            {
                AxisStatius[1] = 2;
            }

            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisX_HomeDone", ref AxisGetstatusHome);  //bit2:bit3 是否寻零（1正在寻零，2寻零完成） 

            if (AxisGetstatusHome == 1)
            {
                AxisStatius[2] = 8;
            }
            else
            {
                AxisStatius[2] = 4;
            }

            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxis_XSine_MoveDone", ref AxisGetstatusHome);  //bit4:bit5 是否正弦

            if (AxisGetstatusHome == 0)
            {
                AxisStatius[3] = 0;
            }
            else
            {
                AxisStatius[3] = 16;
            }
            ReturnStatus = (byte)(AxisStatius[0] + AxisStatius[1] + AxisStatius[2] + AxisStatius[3]);

            return ReturnStatus;
        }

        private string GetLocalIP()
        {
            NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;
            string mip;

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                if (ni.Name == "VMware Network Adapter VMnet1")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();

                            return mip;

                        }
                    }
                }

            }
            return null;
        }
        private string GetLocalIP2()
        {
            NetworkInterface[] interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
            int len = interfaces.Length;
            string mip;

            for (int i = 0; i < len; i++)
            {
                NetworkInterface ni = interfaces[i];

                if (ni.Name == "VMware Network Adapter VMnet8")
                {
                    IPInterfaceProperties property = ni.GetIPProperties();
                    foreach (UnicastIPAddressInformation ip in property.UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            mip = ip.Address.ToString();

                            return mip;

                        }
                    }
                }

            }
            return null;
        }
        public void WritrToRemove()
        {
            while(true)
            {
                

                float hypos = 100.0f;
                float hysp = 20f;
                float zypos = 100.0f;
                float zysp = 20f;
                float gzpos = 50.0f; //滚转轴速度不要
                //数据Data的格式，长度20，Data[0]=0xeb,Data[19]=0xff,Data[18]=为校验和
                //Data[1],Data[2],Data[3] 表示轴的状态，bit0 是否在线(1在线 0不在线) bit1是否运动（1运动，0停止） bit2:bit3 是否寻零（1正在寻零，2寻零完成） bit4:bit5 是否正弦
                //Data[4-7] 表示横移位置值*100，毫米
                //Data[8-9] 表示横移速度值*10 毫米/秒
                //Data[10-13] 表示纵移位置值*100 毫米
                //Data[14-15] 表示纵移速度值*10 毫米/秒
                //Data[16-17] 表示滚转位置值*10 度
                byte[] Data = new byte[20];
                Data[0] = 0xeb; //头
                Data[19] = 0xff; //尾
                Data[1] = AxisStatiusNow[0]; //横移轴状态bit0 是否在线(1在线 0不在线) bit1是否运动（1运动，0停止） 
                //bit2:bit3 是否寻零（1正在寻零，2寻零完成） bit4:bit5 是否正弦（1正弦，0线性）
                Data[2] = AxisStatiusNow[1]; //纵移移轴状态
                Data[3] = AxisStatiusNow[2]; //极化轴状态
                byte[] temp = new byte[4];

                int ret = 0;
                float[] f_AxisPara = new float[10];
                int[] i_AxisPara = new int[10];
                if ((g_handle != (IntPtr)0))
                {
                    for (int i = 0; i < 3; i++)
                    {
                        ret += zmcaux.ZAux_Direct_GetMpos(g_handle, i, ref f_AxisPara[i * 3]);
                        ret += zmcaux.ZAux_Direct_GetMspeed(g_handle, i, ref f_AxisPara[i * 3 + 1]);
                        if (ret == 0)
                        {
                            if (i == 0)
                            {
                                //Btn_StrvoOn_X.Text = (i_AxisPara[i] == 0) ? "使能" : "禁能";
                                //labelXEnable.Text = (i_AxisPara[i] == 0) ? "未使能" : "已使能";
                                hypos = f_AxisPara[i * 3];
                                hysp = f_AxisPara[i * 3 + 1];
                            }
                            if(i == 1)
                            {
                                zypos = f_AxisPara[i * 3];
                                zysp = f_AxisPara[i * 3 + 1];
                            }
                            if(i == 2)
                            {
                                gzpos = f_AxisPara[i * 3] / 10000;
                            }
                        }
                    }
                }
                


              
                Array.Resize(ref temp, 4);
                Array.Clear(temp, 0, 4);
                temp = BitConverter.GetBytes((int)(hypos*100f));
                for(int i=0;i<4;i++)
                {
                    Data[4 + i] = temp[i];  //横移轴位置
                }
                Array.Resize(ref temp, 2);
                Array.Clear(temp, 0, 2);
                temp = BitConverter.GetBytes((short)(hysp * 10f));
                for (int i = 0; i < 2; i++)
                {
                    Data[8 + i] = temp[i];  //横移速度
                }
                Array.Resize(ref temp, 4);
                Array.Clear(temp, 0, 4);
                temp = BitConverter.GetBytes((int)(zypos * 100f));
                for (int i = 0; i < 4; i++)
                {
                    Data[10 + i] = temp[i];  //纵移轴位置
                }
                Array.Resize(ref temp, 2);
                Array.Clear(temp, 0, 2);
                temp = BitConverter.GetBytes((short)(zysp * 10f));
                for (int i = 0; i < 2; i++)
                {
                    Data[14 + i] = temp[i];  //纵移速度
                }
                Array.Resize(ref temp, 2);
                Array.Clear(temp, 0, 2);
                temp = BitConverter.GetBytes((short)(gzpos * 10f));
                for (int i = 0; i < 2; i++)
                {
                    Data[16 + i] = temp[i];  //滚转轴位置
                }
            

                byte sum = 0;
                for (int i = 1; i < 18; i++)
                {
                    sum += Data[i];
                }
                Data[18] = sum; //校验和
                RC.SendData(Data);
                Thread.Sleep(Scan_time_ms);
            }
        }

        private void Btn_Exit_Click(object sender, EventArgs e)
        {
            timer_Connect.Enabled = false;
            zmcaux.ZAux_Close(g_handle);
            g_handle = (IntPtr)0;
            this.Text = "未连接";
            Application.Exit();
        }


        private void Btn_DisConnect_Click(object sender, EventArgs e)
        {
            float cmd = 1;
            zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送命令
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            //RC.Close();
            //RC.StopAxishomeThread();
            //WriteThread.Abort();
            //WriteThread.Join();

            timer_Connect.Enabled = false;
            zmcaux.ZAux_Close(g_handle);
            g_handle = (IntPtr)0;
            Environment.Exit(0);
            //Application.Exit();
        }

        private void Btn_Connect_Click_1(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                Btn_Connect.Text = "未连接";
                Btn_Connect.Image = global::HTCP_Scan_Frame.Properties.Resources.Disconnect;
                //timer_Connect.Enabled = false;
                zmcaux.ZAux_Close(g_handle);
                g_handle = (IntPtr)0;
                zmcaux.ZAux_Direct_SetOp(g_handle, 7, 0);
            }
            //zmcaux.ZAux_OpenCom(1, out g_handle);
            zmcaux.ZAux_OpenEth("192.168.0.11", out g_handle);
            if (g_handle != (IntPtr)0)
            {
                Btn_Connect.Text = "已连接";
                Btn_Connect.Image = global::HTCP_Scan_Frame.Properties.Resources.Connect;
                //timer_Connect.Enabled = true;
                zmcaux.ZAux_Direct_SetOp(g_handle, 7, 1);

            }
            else
            {
                scrollingText_Notioce.ScrollText = "系统未连接，请检查控制器是否上电";
            }

        }

        private void timer_Connect_Tick(object sender, EventArgs e)
        {
            int retc = 0;
            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//读取BAS文件中的变量判断是否有加载BAS文件
            if (ret == 0 && Bus_Connect == 1)
            {
                if (Bus_ConnectRead == false )
                {
                    Bus_ConnectRead = true;
                    Btn_Connect.Text = "已连接";
                    Btn_Connect.Image = global::HTCP_Scan_Frame.Properties.Resources.Connect;
                    ret = zmcaux.ZAux_Direct_SetOp(g_handle, 7,1);
                }
                ret = zmcaux.ZAux_Direct_SetOp(g_handle, 7, 1);
                zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gBusConnectCheck", 0);          //发送连接判断
                
            }
            else
            {
                if (g_handle == (IntPtr)0)
                {
                    zmcaux.ZAux_Close(g_handle);
                    g_handle = (IntPtr)0;
                }
                if (Bus_ConnectRead == true)
                {
                    Bus_ConnectRead = false;
                    Btn_Connect.Image = global::HTCP_Scan_Frame.Properties.Resources.Disconnect;
                    Btn_Connect.Text = "未连接";
                    ret = zmcaux.ZAux_Direct_SetOp(g_handle, 7, 0);
                }
                retc = zmcaux.ZAux_OpenEth("192.168.0.11", out g_handle);
                //retc = zmcaux.ZAux_OpenCom(1, out g_handle);
                if (retc == 0)
                {
                    if (g_handle != (IntPtr)0)
                    {
                        Btn_Connect.Text = "已连接";
                        Btn_Connect.Image = global::HTCP_Scan_Frame.Properties.Resources.Connect;
                        ret = zmcaux.ZAux_Direct_SetOp(g_handle, 7, 1);
                        zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gBusConnectCheck", 0);          //发送连接判断

                    }
                }
               
            }
        }

        private void timer_Show_Tick(object sender, EventArgs e)
        {
            bool bConnect = false;
            label_date.Text = DateTime.Now.ToLongDateString();
            label_timer.Text = DateTime.Now.ToLongTimeString();
            int ret = 0;
            float[] f_AxisPara = new float[10];
            int[] i_AxisPara = new int[10];
            if (scrollingText_Notioce.ScrollText != "")
            {
                AlarmShowTimeNum++;
                if (AlarmShowTimeNum > AlarmShowTime)
                {
                    AlarmShowTimeNum = 0;
                    scrollingText_Notioce.ScrollText = "";
                }

            }


            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//读取BAS文件中的变量判断是否有加载BAS文件
            if (ret == 0 && Bus_Connect == 1)
            {
                bConnect = true;

            }

            if (bConnect == true)
            {
                if (g_Alarm != "")
                {
                    scrollingText_Notioce.ScrollText = g_Alarm;
                    g_Alarm = "";
                }
                for (int i = 0; i < 3; i++)
                {
                    ret += zmcaux.ZAux_Direct_GetAxisEnable(g_handle, i, ref i_AxisPara[i]);
                    ret += zmcaux.ZAux_Direct_GetMpos(g_handle, i, ref f_AxisPara[i * 3]);
                    ret += zmcaux.ZAux_Direct_GetMspeed(g_handle, i, ref f_AxisPara[i * 3 + 1]);
                    ret += zmcaux.ZAux_Direct_GetAxisStatus(g_handle, i, ref i_AxisPara[i * 3 + 2]);
                    if (ret == 0)
                    {
                        if (i == 0)
                        {
                            Btn_StrvoOn_X.Text = (i_AxisPara[i] == 0) ? "使能" : "禁能";
                            labelXEnable.Text = (i_AxisPara[i] == 0) ? "未使能" : "已使能";
                            //g_XSrevoOn
                            labelXPOS.Text = f_AxisPara[i * 3].ToString("#0.000") + "mm";
                            labelXSpeed.Text = f_AxisPara[i * 3 + 1].ToString("#0.000") + "mm/s";
                            if (i_AxisPara[i * 3 + 2] != 0)
                            {
                                if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == false))
                                {
                                    Btn_Home_X.Text = "回零中";
                                    //ifHome[i] = true;
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 8) == 8))
                                {
                                    scrollingText_Notioce.ScrollText = "横移轴驱动器报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 16) == 16))			//第6位是否被置1
                                {
                                    if (((i_AxisPara[i * 3 + 2] & 64) != 64))
                                    {
                                        scrollingText_Notioce.ScrollText = "横移轴正限位报警!";
                                    }

                                }
                                else if (((i_AxisPara[i * 3 + 2] & 32) == 32))
                                {
                                    scrollingText_Notioce.ScrollText = "横移轴负限位报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 4194304) == 4194304))
                                {
                                    scrollingText_Notioce.ScrollText = "急停报警!";
                                }
                                else
                                {
                                    if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == true))
                                    {

                                    }
                                    else
                                    {
                                        if ((i_AxisPara[i * 3 + 2] != 4) && (i_AxisPara[i * 3 + 2] != 2048))
                                        {
                                            scrollingText_Notioce.ScrollText = "横移轴报警，报警代码:" + i_AxisPara[i * 3 + 2].ToString();
                                        }

                                    }
                                }
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisX_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {

                                }
                                else
                                {
                                    Btn_Home_X.Text = "未回零";
                                }
                            }
                            else
                            {
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisX_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {
                                    Btn_Home_X.Text = "回零完成";
                                    //ifHome[i] = false;
                                }
                                else
                                {
                                    Btn_Home_X.Text = "未回零";
                                }
                                //if (ifHome[i] == true)
                                //{
                                    

                                //}
                            }
                        }
                        if (i == 1)
                        {
                            Btn_StrvoOn_Y.Text = (i_AxisPara[i] == 0) ? "使能" : "禁能";
                            labelYEnable.Text = (i_AxisPara[i] == 0) ? "未使能" : "已使能";
                            labelYPOS.Text = f_AxisPara[i * 3].ToString("#0.000") + "mm";
                            labelYSpeed.Text = f_AxisPara[i * 3 + 1].ToString("#0.000") + "mm/s";
                            if (i_AxisPara[i * 3 + 2] != 0)
                            {
                                if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == false))
                                {
                                    //Btn_Home_Y.Text = "回零中";
                                    //ifHome[i] = true;
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 8) == 8))
                                {
                                    scrollingText_Notioce.ScrollText = "纵移轴驱动器报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 16) == 16))			//第6位是否被置1
                                {
                                    if (((i_AxisPara[i * 3 + 2] & 64) != 64))
                                    {
                                        scrollingText_Notioce.ScrollText = "纵移轴正限位报警!";
                                    }
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 32) == 32))
                                {
                                    scrollingText_Notioce.ScrollText = "纵移轴负限位报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 4194304) == 4194304))
                                {
                                    scrollingText_Notioce.ScrollText = "急停报警!";
                                }
                                else
                                {
                                    if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == true))
                                    {

                                    }
                                    else
                                    {
                                        if ((i_AxisPara[i * 3 + 2] != 4) && (i_AxisPara[i * 3 + 2] != 2048))
                                        {
                                            scrollingText_Notioce.ScrollText = "纵移轴报警，报警代码:" + i_AxisPara[i * 3 + 2].ToString();
                                        }

                                    }

                                }
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisY_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {

                                }
                                else
                                {
                                    Btn_Home_Y.Text = "未回零";
                                }
                            }
                            else
                            {
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisY_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {
                                    Btn_Home_Y.Text = "回零完成";
                                    //ifHome[i] = false;
                                }
                                else
                                {
                                    Btn_Home_Y.Text = "未回零";
                                }
                                //if (ifHome[i] == true)
                                //{


                                //}
                            }
                        }
                        if (i == 2)
                        {
                            Btn_StrvoOn_Z.Text = (i_AxisPara[i] == 0) ? "使能" : "禁能";
                            labelZEnable.Text = (i_AxisPara[i] == 0) ? "未使能" : "已使能";
                            labelZPOS.Text = (f_AxisPara[i * 3] / 10000).ToString("#0.000") + "°";
                            labelZSpeed.Text = (f_AxisPara[i * 3 + 1] / 10000).ToString("#0.000") + "°/s";
                            if (i_AxisPara[i * 3 + 2] != 0)
                            {
                                if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == false))
                                {
                                    //Btn_Home_Z.Text = "回零中";
                                    //ifHome[i] = true;
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 8) == 8))
                                {
                                    scrollingText_Notioce.ScrollText = "极化轴驱动器报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 16) == 16) && ((i_AxisPara[i * 3 + 2] & 64) != 64))			//第6位是否被置1
                                {
                                    scrollingText_Notioce.ScrollText = "极化轴正限位报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 32) == 32))
                                {
                                    scrollingText_Notioce.ScrollText = "极化轴负限位报警!";
                                }
                                else if (((i_AxisPara[i * 3 + 2] & 4194304) == 4194304))
                                {
                                    scrollingText_Notioce.ScrollText = "急停报警!";
                                }
                                else
                                {
                                    if (((i_AxisPara[i * 3 + 2] & 64) == 64) && (ifHome[i] == true))
                                    {

                                    }
                                    else
                                    {
                                        if ((i_AxisPara[i * 3 + 2] != 64) && (i_AxisPara[i * 3 + 2] != 2048))
                                        {
                                            scrollingText_Notioce.ScrollText = "极化轴报警，报警代码:" + i_AxisPara[i * 3 + 2].ToString();
                                        }

                                    }
                                }
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisZ_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {

                                }
                                else
                                {
                                    Btn_Home_Y.Text = "未回零";
                                }
                            }
                            else
                            {
                                float homeStatus = 0;
                                ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisZ_HomeDone", ref homeStatus);
                                if (homeStatus == 1)
                                {
                                    Btn_Home_Z.Text = "回零完成";
                                    //ifHome[i] = false;
                                }
                                else
                                {
                                    Btn_Home_Z.Text = "未回零";
                                }
                                //if (ifHome[i] == true)
                                //{
                                    

                                //}
                            }
                        }

                    }
                }
            }

            
        }

        public void StopAxis()
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            ret = zmcaux.ZAux_Direct_Rapidstop(g_handle, 2);
            if (ret != 0)
            {
                scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                return;
            }
        }


        //横移轴JOG+
        private void Btn_JOG_XP_MouseDown(object sender, MouseEventArgs e)    
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if (g_HomeX)
                {
                    scrollingText_Notioce.ScrollText = "横移轴在回零中";
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 0, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString(); 
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 0, Convert.ToSingle(textBox_JOG_X.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 0, 5 * Convert.ToSingle(textBox_JOG_X.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 0, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
            
        }    

        private void Btn_JOG_XP_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeX)
            {
                scrollingText_Notioce.ScrollText = "横移轴在回零中";
                return;
            }
            StopXAxis();
        }

        private void Btn_JOG_XN_MouseDown(object sender, MouseEventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if (g_HomeX)
                {
                    scrollingText_Notioce.ScrollText = "横移轴在回零中";
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 0, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 0, Convert.ToSingle(textBox_JOG_X.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 0, 5 * Convert.ToSingle(textBox_JOG_X.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 0, -1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
        }

        private void Btn_JOG_XN_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeX)
            {
                scrollingText_Notioce.ScrollText = "横移轴在回零中";
                return;
            }
            StopXAxis();
        }

        private void Btn_JOG_YP_MouseDown(object sender, MouseEventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if (g_HomeY)
                {
                    scrollingText_Notioce.ScrollText = "纵移轴在回零中";
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 1, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 1, Convert.ToSingle(textBox_JOG_Y.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 1, 5 * Convert.ToSingle(textBox_JOG_Y.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 1, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
        }

        private void Btn_JOG_YP_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeY)
            {
                scrollingText_Notioce.ScrollText = "纵移轴在回零中";
                return;
            }
            StopYAxis();
        }

        private void Btn_JOG_YN_MouseDown(object sender, MouseEventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if (g_HomeY)
                {
                    scrollingText_Notioce.ScrollText = "纵移轴在回零中";
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 1, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 1, Convert.ToSingle(textBox_JOG_Y.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 1, 5 * Convert.ToSingle(textBox_JOG_Y.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 1, -1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
        }

        private void Btn_JOG_YN_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeY)
            {
                scrollingText_Notioce.ScrollText = "纵移轴在回零中";
                return;
            }
            StopYAxis();
        }

        private void Btn_JOG_ZP_MouseDown(object sender, MouseEventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if (g_HomeZ)
                {
                    scrollingText_Notioce.ScrollText = "极化轴在回零中";
                    return;
                }

                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 2, 10000 * Convert.ToSingle(textBox_JOG_Z.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 2, 50000 * Convert.ToSingle(textBox_JOG_Z.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
        }

        private void Btn_JOG_ZP_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeZ)
            {
                scrollingText_Notioce.ScrollText = "极化轴在回零中";
                return;
            }
            StopZAxis();
        }

        private void Btn_JOG_ZN_MouseDown(object sender, MouseEventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            try
            {
                if(g_HomeZ)
                {
                    scrollingText_Notioce.ScrollText = "极化轴在回零中";
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 2, 10000 * Convert.ToSingle(textBox_JOG_Z.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 2, 50000 * Convert.ToSingle(textBox_JOG_Z.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Vmove(g_handle, 2, -1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "JOG速度输入非法";
                return;
            }
        }

        private void Btn_JOG_ZN_MouseUp(object sender, MouseEventArgs e)
        {
            if (g_HomeZ)
            {
                scrollingText_Notioce.ScrollText = "极化轴在回零中";
                return;
            }
            StopZAxis();
        }

        private void Btn_Clear_Alarm_Click(object sender, EventArgs e)
        {
            scrollingText_Notioce.ScrollText = null;
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            ret = zmcaux.ZAux_BusCmd_DriveClear(g_handle, 0, 0);
            ret = zmcaux.ZAux_BusCmd_DriveClear(g_handle, 1, 0);
            ret = zmcaux.ZAux_BusCmd_DriveClear(g_handle, 2, 0);
        }




        private void Btn_Home_X_Click(object sender, EventArgs e)
        {
            //UpdateWriteData("X_Axis_Offset", "-500");
            //UpdateWriteData("Y_Axis_Offset", "-500");
            //UpdateWriteData("Z_Axis_Offset", "10");
            UpdateWriteData("Alarm_Show_time_s", "40");

            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            try
            {
                Btn_Home_X.Text = "未回零";
                zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gAxisX_HomeDone", 0);          //发送幅值
                if (g_HomeX == false)
                {
                    StopXAxis();
                    g_HomeX = true;
                    RC.HomeStepX = 10;
                }
                
                //float cmd = 0;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxisX_HomeDone", cmd);          //发送命令
                //cmd = 0;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gHomeStep", cmd);          //发送命令
                //cmd = 5;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送命令
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "控制器程序错误，无对应变量!";
            }
            
        }

        private void Btn_Home_Y_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            try
           
            {
                Btn_Home_Y.Text = "未回零";
                zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gAxisY_HomeDone", 0);          //发送幅值
                if (g_HomeY == false)
                {
                    StopYAxis();
                    g_HomeY = true;
                    RC.HomeStepY = 10;
                }
                
                //float cmd = 0;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxisY_HomeDone", cmd);          //发送命令
                //cmd = 1;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gHomeStep", cmd);          //发送命令
                //cmd = 5;
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送命令
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "控制器程序错误，无对应变量!";
            }
        }

        private void Btn_Home_Z_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            try
            {
                Btn_Home_Z.Text = "未回零";
                zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gAxisZ_HomeDone", 0);          //发送幅值
                if (g_HomeZ == false)
                {
                    StopZAxis();
                    g_HomeZ = true;
                    RC.HomeStepZ = 10;
                }
                
                //if (Form1.g_handle == (IntPtr)0)
                //{
                //    Form1.g_Alarm = "未链接到控制器!";
                //    return;
                //}
                //int ret = 0;
                //ret = zmcaux.ZAux_Direct_SetUnits(Form1.g_handle, 2, 1);
                //ret = zmcaux.ZAux_Direct_SetAccel(Form1.g_handle, 2, 1000000);
                //ret = zmcaux.ZAux_Direct_SetSpeed(Form1.g_handle, 2, 200000);
                //ret = zmcaux.ZAux_Direct_SetCreep(Form1.g_handle, 2, 100000);
                //ret = zmcaux.ZAux_BusCmd_SetDatumOffpos(Form1.g_handle, 2, 0);
                //ret = zmcaux.ZAux_BusCmd_Datum(Form1.g_handle, 2, Convert.ToUInt16 (textBox_JOG_Z.Text));
                return;
                //float cmd = 0;
                //Thread.Sleep(20);
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxisZ_HomeDone", cmd);          //发送命令
                //cmd = 2;
                //Thread.Sleep(20);
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gHomeStep", cmd);          //发送命令
                //cmd = 5;
                //Thread.Sleep(20);
                //zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送命令
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "控制器程序错误，无对应变量!";
            }
        }

        private void Btn_StrvoOn_X_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
               scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;

            if (Btn_StrvoOn_X.Text == "禁能")
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 0, 0);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            else
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 0, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
        }

        private void Btn_StrvoOn_Y_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;

            if (Btn_StrvoOn_Y.Text == "禁能")
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 1, 0);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            else
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 1, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
        }

        private void Btn_StrvoOn_Z_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;

            if (Btn_StrvoOn_Z.Text == "禁能")
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 2, 0);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            else
            {
                ret = zmcaux.ZAux_Direct_SetAxisEnable(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
        }

        private void Btn_MoveAbs_X_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopXAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 0, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 0, Convert.ToSingle(textBox_X_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 0, 3 * Convert.ToSingle(textBox_X_Speed.Text.ToString()));
                ret = zmcaux.ZAux_Direct_SetDecel(g_handle, 0, 3 * Convert.ToSingle(textBox_X_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_MoveAbs(g_handle, 0, Convert.ToSingle(textBox_X_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }
        }

        private void Btn_Move_X_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopXAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 0, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 0, Convert.ToSingle(textBox_X_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 0, 3 * Convert.ToSingle(textBox_X_Speed.Text.ToString()));  //ZAux_Direct_SetDecel
                ret = zmcaux.ZAux_Direct_SetDecel(g_handle, 0, 3 * Convert.ToSingle(textBox_X_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }

                ret = zmcaux.ZAux_Direct_Single_Move(g_handle, 0, Convert.ToSingle(textBox_X_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }

        }

        private void Btn_MoveAbs_Y_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelYEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopYAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 1, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 1, Convert.ToSingle(textBox_Y_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 1, 3 * Convert.ToSingle(textBox_Y_Speed.Text.ToString()));
                ret = zmcaux.ZAux_Direct_SetDecel(g_handle, 1, 3 * Convert.ToSingle(textBox_Y_Speed.Text.ToString()));
                
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_MoveAbs(g_handle, 1, Convert.ToSingle(textBox_Y_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }
        }

        private void Btn_Move_Y_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelYEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopYAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 1, 10000);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 1, Convert.ToSingle(textBox_Y_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 1, 3 * Convert.ToSingle(textBox_Y_Speed.Text.ToString()));  //ZAux_Direct_SetDecel
                ret = zmcaux.ZAux_Direct_SetDecel(g_handle, 1, 3 * Convert.ToSingle(textBox_Y_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Move(g_handle, 1, Convert.ToSingle(textBox_Y_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }
        }

        private void Btn_MoveAbs_Z_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelZEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopZAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 2, 10000 * Convert.ToSingle(textBox_Z_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 2, 50000 * Convert.ToSingle(textBox_Z_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_MoveAbs(g_handle, 2, 10000 * Convert.ToSingle(textBox_Z_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }
        }

        private void Btn_Move_Z_Click(object sender, EventArgs e)
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelZEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "未使能";
                return;
            }
            int ret = 0;
            try
            {
                StopZAxis();
                ret = zmcaux.ZAux_Direct_SetUnits(g_handle, 2, 1);
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetSpeed(g_handle, 2, 10000 * Convert.ToSingle(textBox_Z_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_SetAccel(g_handle, 2, 50000 * Convert.ToSingle(textBox_Z_Speed.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
                ret = zmcaux.ZAux_Direct_Single_Move(g_handle, 2, 10000 * Convert.ToSingle(textBox_Z_POS.Text.ToString()));
                if (ret != 0)
                {
                    scrollingText_Notioce.ScrollText = "函数执行错误，错误返回值为：" + ret.ToString();
                    return;
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "速度或者位置输入非法";
                return;
            }
        }

        private void Btn_X_Sine_Start_Click(object sender, EventArgs e)
        {
            float AxisX_HomeDone = 0;
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelXEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "横移轴未使能";
                return;
            }
            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisX_HomeDone", ref AxisX_HomeDone);
            if (AxisX_HomeDone != 1)
            {
                scrollingText_Notioce.ScrollText = "X轴未回零";
                return;
            }
            try
            {
                float cmd = Convert.ToSingle( textBox_X_Amplitude.Text.ToString());
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Amplitudem", cmd);          //发送幅值
                cmd = Convert.ToSingle(textBox_X_Hz.Text.ToString()); 
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Cycles_Time", cmd);          //发送频率
                cmd = Convert.ToSingle(textBox_X_Times.Text.ToString()); 
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Cycles_Num", cmd);          //发送运行周期数
                                                                

                zmcaux.ZAux_Direct_GetUserVar(Form1.g_handle, "gStep", ref cmd);
                if (cmd == 3 || cmd == 4 || cmd == 20)
                {
                    scrollingText_Notioce.ScrollText = "纵移正在正弦运动";
                }
                else
                {
                    cmd = 1;
                    zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gStep", cmd);           //进入正弦运动
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "参数输入错误错误";
            }
        }

        private void Btn_Y_Sine_Start_Click(object sender, EventArgs e)
        {
            float AxisY_HomeDone = 0;
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            if (labelYEnable.Text == "未使能")
            {
                scrollingText_Notioce.ScrollText = "纵移轴未使能";
                return;
            }
            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gAxisY_HomeDone", ref AxisY_HomeDone);
            if (AxisY_HomeDone != 1)
            {
                scrollingText_Notioce.ScrollText = "纵移轴未回零";
                return;
            }
            try
            {
                float cmd = Convert.ToSingle(textBox_Y_Amplitude.Text.ToString());
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Amplitudem", cmd);          //发送幅值
                cmd = Convert.ToSingle(textBox_Y_Hz.Text.ToString());
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Cycles_Time", cmd);          //发送频率
                cmd = Convert.ToSingle(textBox_Y_Times.Text.ToString());
                zmcaux.ZAux_Direct_SetUserVar(g_handle, "gAxis_Cycles_Num", cmd);          //发送运行周期数
                
                
                zmcaux.ZAux_Direct_GetUserVar(Form1.g_handle, "gStep", ref cmd);
                if (cmd == 1 || cmd == 2 || cmd == 10)
                {
                    scrollingText_Notioce.ScrollText = "横移正在正弦运动";
                }
                else
                {
                    cmd = 3;
                    zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gStep", cmd);          //发送运行周期数
                }
            }
            catch
            {
                scrollingText_Notioce.ScrollText = "参数输入错误错误";
            }
        }

        private void Btn_CON_RM_Click(object sender, EventArgs e)
        {
            RC.Start(myIP, 60310, myIP2, 60310);
            if (RC.IsOpened == true)
            {
                Btn_CON_RM.Image = global::HTCP_Scan_Frame.Properties.Resources.Connect;
                Btn_CON_RM.Text = "连接";
                Btn_CON_RM.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(16)))), ((int)(((byte)(34)))), ((int)(((byte)(44)))));
            }
            else
            {
                Btn_CON_RM.Image = global::HTCP_Scan_Frame.Properties.Resources.Disconnect;
                Btn_CON_RM.Text = "断开";
                Btn_CON_RM.BackColor = Color.Maroon;
            }
        }

        public void StopAllAxis()
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            g_HomeX = false;
            RC.HomeStepX = 0;
            g_HomeY = false;
            RC.HomeStepY = 0;
            g_HomeZ = false;
            RC.HomeStepZ = 0;

            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 0, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 0, 3);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 1, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 1, 3);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 2, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 2, 3);
            ret = zmcaux.ZAux_Direct_Rapidstop(g_handle, 2);
            float cmd = 0;                                                                 //进入正弦运动
            zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送运行周期数
        }

        public void StopXAxis()
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 0, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 0, 3);
            float cmd = 0;                                                                 //进入正弦运动
            zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送运行周期数
        }

        public void StopYAxis()
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 1, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 1, 3);
            float cmd = 0;                                                                 //进入正弦运动
            zmcaux.ZAux_Direct_SetUserVar(g_handle, "gStep", cmd);          //发送运行周期数
        }

        public void StopZAxis()
        {
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = 0;
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 2, 2);
            ret = zmcaux.ZAux_Direct_Single_Cancel(g_handle, 2, 3);
        }

        private void BtnStop_Click(object sender, EventArgs e)
        {
            StopAllAxis();
        }

        private void timer_Status_Tick(object sender, EventArgs e)
        {
            bool bConnect = false;
            if (g_handle == (IntPtr)0)
            {
                scrollingText_Notioce.ScrollText = "未链接到控制器!";
                return;
            }
            int ret = zmcaux.ZAux_Direct_GetUserVar(g_handle, "gBusConnect", ref Bus_Connect);						//读取BAS文件中的变量判断是否有加载BAS文件
            if (ret == 0 && Bus_Connect == 1)
            {
                bConnect = true;

            }

            if (bConnect == true)
            {
                zmcaux.ZAux_Direct_SetUserVar(Form1.g_handle, "gBusConnectCheck", 0);          //发送连接判断
                ret = zmcaux.ZAux_Direct_SetOp(g_handle, 7, 1);
                AxisStatiusNow[0] = GetAxisXStatius();
                AxisStatiusNow[1] = GetAxisYStatius();
                AxisStatiusNow[2] = GetAxisZStatius(); 
            }
        }
    }
}
